<template>
  <!-- 登录界面 -->
  <div class="form-all">
    <!-- 标题与表单输入框 -->
    <div class="form-wrapper">
      <!-- logo部分 -->
      <div class="header">
        <img src="@/assets/logo.png" />
      </div>
      <!-- 表单 -->
      <div class="input-wrapper">
        <div class="border-wrapper">
          <input
            type="text"
            placeholder="请输入管理员账号"
            class="border-item"
            autocomplete="off"
            v-model="formData.admin"
          />
        </div>
        <div class="border-wrapper">
          <input
            type="password"
            placeholder="请输入管理员密码"
            class="border-item"
            autocomplete="off"
            v-model="formData.password"
          />
        </div>
      </div>
      <!-- 登录按钮 -->
      <div class="action">
        <div class="btn" @click="login">登录</div>
      </div>
      <!-- 第三方登录 -->
      <div class="icon-wrapper">
        <i class="el-icon-edit"></i>
        <i class="el-icon-chat-dot-round"></i>
        <i class="el-icon-mobile-phone"></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        admin: "",
        password: ""
      }
    };
  },
  methods: {
    // 登录方法
    login() {
      // 非空校验
      if (this.formData.admin.length == 0) {
        this.$message.error("账号不能为空！");
        return;
      }
      if (this.formData.password.length == 0) {
        this.$message.error("密码不能为空！");
        return;
      }
      // 封装body信息
      // axios提交表单，需要将表单数据转换为FormData对象
      let data = new FormData();
      data.append("d_admin", this.formData.admin);
      data.append("d_pwd", this.formData.password);
      // 请求后端服务器
      this.$http
        .post(
          this.$api.login,
          //`d_admin=${this.formData.admin}&&d_pwd=${this.formData.password}` // 写法一
          // 写法二，需要后端配置 内容解析器，重要的事情说三遍，需要后端支持 1,2,3
          {
            d_admin: this.formData.admin,
            d_pwd: this.formData.password
          }
        )
        .then(res => {
          console.log("登录成功返回：", res);
          if (res?.data?.code == 200) {
            // 将token保存到本地session中
            sessionStorage.setItem("token", res?.data?.data?.token);
            // 将登录成功的用户信息保存到session中
            sessionStorage.setItem("admin", JSON.stringify(res?.data?.data));
            // 消息提示
            this.$message({
              message: res?.data?.msg,
              type: "success"
            });
            // 跳转到主界面
            this.$router.replace("/home");
          } else {
            this.$message.error(res?.data?.msg);
          }
        })
        .catch(err => {
          console.log("登录失败返回：", err);
          this.$message.error(err);
        });
    }
  }
};
</script>

<style scoped>
.form-all {
  height: 100%;
  font-family: JetBrains Mono Medium;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #b3d8ff;
  background-size: 100% 100%;
}
.form-wrapper {
  width: 300px;
  background-color: rgba(41, 45, 62, 0.8);
  color: #fff;
  border-radius: 10px;
  padding: 25px;
}
.form-wrapper .header img {
  width: 65%;
  margin-bottom: 20px;
}
.form-wrapper .input-wrapper input {
  background-color: rgb(41, 45, 62);
  border: 0;
  width: 100%;
  text-align: center;
  font-size: 15px;
  color: #fff;
  outline: none;
}
.form-wrapper .input-wrapper input::placeholder {
  text-transform: uppercase;
}
.form-wrapper .input-wrapper .border-wrapper {
  background-image: linear-gradient(to right, #fff, #0eb4dd);
  width: 100%;
  height: 50px;
  margin-bottom: 20px;
  border-radius: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.form-wrapper .input-wrapper .border-wrapper .border-item {
  height: calc(100% - 4px);
  width: calc(100% - 4px);
  border-radius: 30px;
}
.form-wrapper .action {
  display: flex;
  justify-content: center;
}
.form-wrapper .action .btn {
  width: 100%;
  height: 46px;
  text-transform: uppercase;
  border: 2px solid #0e92b3;
  text-align: center;
  line-height: 46px;
  border-radius: 30px;
  cursor: pointer;
  user-select: none;
}
.form-wrapper .action .btn:active {
  background-image: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
}
.form-wrapper .icon-wrapper {
  text-align: center;
  width: calc(100% - 50px);
  margin: 0 auto;
  margin-top: 20px;
  border-top: 1px dashed rgb(146, 146, 146);
  padding: 20px;
}
.form-wrapper .icon-wrapper i {
  font-size: 20px;
  color: rgb(187, 187, 187);
  cursor: pointer;
  border: 1px solid #fff;
  padding: 5px;
  border-radius: 20px;
  margin: 3px;
}
.form-wrapper .icon-wrapper i:active {
  background-color: #0e92b3;
}
.header {
  text-align: center;
}
</style>